﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LDSP.BRL
{
    public class BaseSysAdminPage : BaseValidUserPage
    {
        public override void RedirectBefore()
        {
            if (LoginAdmin == null)
            {
                this.GoToLogin();
            }
            else if (!LoginAdmin.IsSuper)
            {
                //用户被删除后，即使已登录了也退出
                LoginAdmin.GetProperties(LoginAdmin.ID);
                if (LoginAdmin.ID == 0)
                {
                    this.GoToLogin();
                }
            }
        }
        public override void GoToLogin(bool autoLoginWithCookie)
        {
            if (autoLoginWithCookie)
            {
                this.Response.Write("<script>window.parent.location.href='/UI/AdminLogin.aspx';</script>");
            }
            else
            {
                this.Response.Write("<script>window.parent.location.href='/UI/AdminLogin.aspx?FOBID_AUTO_LOGIN=true';</script>");
            }
        }

        protected List<R_SystemUserCatalog> m_systemUserCatalogs = null;
        protected List<int> m_allowedCatalogIDs = null;

        protected List<R_SystemUserCatalog> SystemUserCatalogs
        {
            get
            {
                if (m_systemUserCatalogs == null)
                {
                    m_systemUserCatalogs = new R_SystemUserCatalog().GetListBySystemUserID(LoginAdmin.ID);
                }
                return m_systemUserCatalogs;
            }
        }
        protected List<int> AllowedCatalogIDs
        {
            get
            {
                if (m_allowedCatalogIDs == null)
                {
                    m_allowedCatalogIDs = new List<int>();
                    for (int i = 0; i < SystemUserCatalogs.Count; i++)
                    {
                        m_allowedCatalogIDs.Add(SystemUserCatalogs[i].CatalogID);
                    }
                }
                return m_allowedCatalogIDs;
            }
        }

        protected bool HaveCatalogPermission(int catalogID)
        {
            return catalogID == 0 || LoginAdmin.IsSuper || AllowedCatalogIDs.Contains(catalogID);
        }

        /// <summary>
        /// 是否允许系统管理员
        /// </summary>
        /// <param name="redirect">是否重定向到默认页</param>
        /// <returns></returns>
        protected bool AllowSystemUser(bool redirect = true)
        {
            if (LoginAdmin != null)
            {
                return true;
            }
            else
            {
                this.JSAlert("您没有访问权限！", true);
                if (redirect)
                {
                    this.JSRedirect("MyPage.aspx", false, true);
                }
                return false;
            }
        }
    }
}